$(function () {
    let username = "";
    let password = "";
    let remember = false;
    // 檢查用戶名
    let $username = $("input[name='telephone']");
    $username.blur(fn_check_username);
    function fn_check_username(){
        username = $username.val();
        if(username.length === 0){
            message.showError("用戶名不能為空");
            return;
        }
        if(!(/^\w{5,20}$/).test(username)){
            message.showError("用戶名格式錯誤");
            return;
        }
        return 1;
    }
    // 密碼驗證
    let $password = $("input[name='password']");
    $password.blur(fn_check_password);
    function fn_check_password(){
        password = $password.val();
        if(password.length === 0){
            message.showError("密碼不能為空");
            return;
        }
        if(!(/^\w{5,20}$/).test(password)){
            message.showError("密碼格式錯誤");
            return;
        }
        return 1;
    }
    // 獲取記住我
    let $remember = $("input[name='remember']");
    let $login = $(".form-contain");

    function getCookie(name){
        let cookieValue = null;
        if(document.cookie && document.cookie != ''){
            let cookies = document.cookie.split(";");
            for(let i=0;i<cookies.length;i++){
                let cookie = jQuery.trim(cookies[i]);
                console.log(cookie)
                // 遍历cookie，寻找csrf-token
                if (cookie.substring(0, name.length+1) === (name+'=')){
                    cookieValue = decodeURIComponent(cookie.substring(name.length+1));
                    break;
                }
            }
        }
        return cookieValue;
    }

    $login.submit(function (e) {
        // 阻止默認提交
        e.preventDefault();
        if(fn_check_username() && fn_check_password()){
            let status = $remember.is(":checked");
            // 获取csrf_token的两种方式
            // 第一种直接取得隐藏的值
            let csrf_token = $("input[name='csrfmiddlewaretoken']").val();
            // 第二种方式从cookie中获取
            csrf_token = getCookie("csrftoken");
            let login_data = {
                "username": username,
                "password": password,
                "remember": status
            };
            $.ajax({
                url: "/login/",
                type: "POST",
                data: JSON.stringify(login_data),
                // 在请求头中构造
                headers: {
                  "X-CSRFToken": csrf_token,
                },
                contentType: "application/json;charset=utf-8",
                dataType: "JSON"
            })
                .done(function (res) {
                    if(res.errno === "0"){
                        message.showSuccess(res.errmsg);
                        setTimeout(function () {
                            window.location.href = "/index/";
                        }, 1500);
                    }
                    else{
                        message.showError(res.errmsg);
                    }
                })
                .fail(function () {
                    message.showError("服務器超時，請刷新重試");
                })
        }

    })
});
